package com.example.yueliang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.yueliang.common.Result;
import com.example.yueliang.pojo.Hotel;
import com.example.yueliang.pojo.HotelReservation;
import com.example.yueliang.pojo.Orders;
import com.example.yueliang.pojo.Tickets;
import com.example.yueliang.service.impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/blindBox")
public class BlindBoxController {

    @Autowired
    private TicketsServiceImpl ticketsService;

    @Autowired
    private OrdersServiceImpl ordersService;

    @Autowired
    private HotelServiceImpl hotelService;

    @Autowired
    private HotelReservationImpl hotelReservationImpl;

    @PostMapping("/ticket")
    public Result insertTicket(HttpServletRequest request, @RequestBody Tickets tickets){
        LambdaQueryWrapper<Tickets> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Tickets::getDepartureStation,tickets.getDepartureStation());
        queryWrapper.like(Tickets::getArrivalStation,tickets.getArrivalStation());
        queryWrapper.like(Tickets::getDepartureTime,tickets.getDepartureTime());

        //查询所有符号要求的车票
        List<Tickets> ticketsList = ticketsService.list(queryWrapper);

        if (ticketsList.size() == 0){
            return Result.error(tickets.getDepartureTime()+"没有找到该天的车票,付款金额将在2小时后退回");
        }

        //生成随机数
        Random random = new Random();
        int i = random.nextInt(ticketsList.size());

        Tickets ticket = ticketsList.get(i);

        LambdaQueryWrapper<Orders> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Orders::getTicketId,ticket.getTicketId());
        queryWrapper1.eq(Orders::getUserId,(int)request.getSession().getAttribute("user"));
        int count = ordersService.count(queryWrapper1);

        if (count != 0){
            return Result.error("该天用户已预订，不能重复预订车票");
        }

        Orders orders = new Orders();
        orders.setTicketId(ticket.getTicketId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setUserId((int)request.getSession().getAttribute("user"));

        ordersService.save(orders);
        return Result.success();
    }

    @GetMapping("/hotel")
    public Result insertHotel(HttpServletRequest request,@RequestParam String hotelAddress,@RequestParam String stayTime){
        LambdaQueryWrapper<Hotel> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(Hotel::getHotelAddress,hotelAddress);

        List<Hotel> hotelList = hotelService.list(queryWrapper);

        if (hotelList.size() == 0){
            return Result.error("没有找到匹配的酒店,预订金额将在2小时后退回");
        }
        //生成随机数
        Random random = new Random();
        int i = random.nextInt(hotelList.size());

        Hotel hotel = hotelList.get(i);

        HotelReservation hotelReservation = new HotelReservation();
        hotelReservation.setHid(hotel.getHid());
        hotelReservation.setUid((int)request.getSession().getAttribute("user"));
        hotelReservation.setStayTime(stayTime);

        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 解析字符串日期为 LocalDate 对象
        LocalDate date = LocalDate.parse(stayTime, formatter);
        // 获取两天后的日期
        LocalDate twoDaysAfter = date.plusDays(2);
        // 将日期转换为字符串
        String leaveTime = twoDaysAfter.format(formatter);

        hotelReservation.setLeaveTime(leaveTime);
        hotelReservation.setTotalPrice(200);
        hotelReservation.setReservationRoom("单人房");

        hotelReservationImpl.save(hotelReservation);
        return Result.success();
    }
}
